<?php
/**
 * Created by PhpStorm.
 * User: lyh
 * Date: 2017/4/15
 * Time: 20:27
 */
namespace Admin\Controller;

class PostController extends CommonController{
    public function index(){
        $area_id=I('area_id','1');//默认不传值为1
        $summary=I('summary','');
        $map['pid']=$area_id;
        $map['summary']=array('like','%'.$summary.'%');
        $Model=M('Areapost');
        $userModel=M('User');
        $areaModel=M('Areaassociation');
        $count= $Model->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $post_info=$Model->order('time desc')->where($map)->limit($Page->firstRow.','.$Page->listRows)->field('id,username,title,time,userid,summary')->select();
        $area_info=$areaModel->field('id,title')->select();
        foreach($post_info as $key=>$value){
            $post_info[$key]['username']=$userModel->where('userid='.'"'.$value['userid'].'"')->getField('username');
            unset($post_info[$key]['userid']);
        }
        $this->assign('area_id',$area_id);
        $this->assign('area_info',$area_info);
        $this->assign('post_info',$post_info);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }
}